package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.dao.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class HospitalServiceImpl  implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;
    //在需要调用的地方直接注入自定义FeignClient接口
    @Autowired
    //TODO:需要注入就要在这个微服务的pom文件加入其他的依赖信息
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, String> paramMap) {
        //fastjson：可以把java任意对象---->String
        //fastjson：可以把String   ---->java任意对象-
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);

       Hospital mongoHospital= hospitalRepository.findByHoscode(hospital.getHoscode());
       if(mongoHospital == null){//添加
           hospital.setCreateTime(new Date());
           hospital.setUpdateTime(new Date());
           hospital.setIsDeleted(0);
           hospital.setStatus(0);
           hospitalRepository.save(hospital);
       }else{ //修改
           hospital.setCreateTime(mongoHospital.getCreateTime());
           hospital.setUpdateTime(new Date());
           hospital.setIsDeleted(mongoHospital.getIsDeleted());
           hospital.setStatus(mongoHospital.getStatus());

           hospital.setId(mongoHospital.getId());
           hospitalRepository.save(hospital);

       }
    }

    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        return hospital;
    }

    //带查询条件的分页
    @Override
    public Page<Hospital> page(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        Hospital hospital = new Hospital();
        hospital.setProvinceCode(hospitalQueryVo.getProvinceCode());//省编号
        hospital.setCityCode(hospitalQueryVo.getCityCode());//市编号
        hospital.setHosname(hospitalQueryVo.getHosname());//医院名字
        hospital.setHostype(hospitalQueryVo.getHostype());
        hospital.setDistrictCode(hospitalQueryVo.getDistrictCode());

        //根据前端传入的条件模糊查询
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("hosname", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<Hospital> example = Example.of(hospital, exampleMatcher);

        //根据创建时间升序规则分页
        PageRequest pageRequest = PageRequest.of(pageNum-1,pageSize, Sort.by(Sort.Order.asc("createTime")));
        Page<Hospital> page = hospitalRepository.findAll(example, pageRequest);
        page.getContent().stream().forEach(hosp->{
            this.packageHospital(hosp);
        });
        return page;
    }


    /**
     * 修改医院列表状态
     */
    @Override
    public void updateStatus(String id, Integer status) {
        //1.先查出医院状态
        Optional<Hospital> optional = hospitalRepository.findById(id);
        Hospital hospital = optional.get();
        //判断状态为0或1时修改状态
        if(status == 0 || status == 1){
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);

        }
    }

    //根据id查询医院的详细信息
    @Override
    public Hospital detail(String id) {
        Optional<Hospital> byId = hospitalRepository.findById(id);
        //将医院的数据转换成对应的文字
        Hospital hospital = byId.get();
        this.packageHospital(hospital);
        return hospital;
    }

    @Override
    public List<Hospital> getHospitalListByHosname(String hosname) {
        List<Hospital> hospitalList = hospitalRepository.findByHosnameLike(hosname);
        for (Hospital hospital : hospitalList) {
            this.packageHospital(hospital);
        }
        return hospitalList;
    }

    @Override
    public Hospital getUserHospitalByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        this.packageHospital(hospital);
        return hospital;
    }

    //取出省市区编号
    private void packageHospital(Hospital hosp){
        //取出省市区编号
        String hostype = hosp.getHostype();
        String provinceCode = hosp.getProvinceCode();
        String cityCode = hosp.getCityCode();
        String districtCode = hosp.getDistrictCode();

        String provinceName = dictFeignClient.getNameByValue(Long.parseLong(provinceCode));//省名
        String cityName = dictFeignClient.getNameByValue(Long.parseLong(cityCode));//市名
        String districtName = dictFeignClient.getNameByValue(Long.parseLong(districtCode));//区名

        //OpenFeign远程调用,获取医院等级对应的字符串
        String hosTypeString = dictFeignClient.getNameByDictCodeAndValue(DictEnum.HOSTYPE.getDictCode(), Long.parseLong(hostype));
        hosp.getParam().put("hosTypeString",hosTypeString);
        hosp.getParam().put("fullAddress",provinceName+cityName+districtName+hosp.getAddress());
    }
}